package com.lc.dao.impl;

import com.lc.bean.Menu;
import com.lc.bean.Role;
import com.lc.bean.Users;
import com.lc.dao.DBUtils;
import com.lc.dao.RoleDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: ManolinCoder
 * @time: 2021/8/24
 */
public class RoleDaoImpl extends DBUtils implements RoleDao {
    //查询所有角色（分页）
    @Override
    public List<Role> getRoleList(int pageIndex, int pageSize) {
        List<Role> roleList = new ArrayList<Role>();
        String sql = "select * from role limit ?,?";
        List params = new ArrayList();
        params.add((pageIndex-1)*pageSize);
        params.add(pageSize);
        resultSet = query(sql,params);
        try {
            while (resultSet.next()){
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                roleList.add(role);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return roleList;
    }

    //查询总条数
    @Override
    public int total() {
        int total = 0;
        String sql = "select count(*) from role";
        resultSet = query(sql,null);
        try {
            while (resultSet.next()){
                total = resultSet.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return total;
    }

    //主键查询
    @Override
    public Role getRoleById(int roleId) {
        Role role = new Role();
        String sql = "select * from role where roleid=?";
        List params = new ArrayList();
        params.add(roleId);
        resultSet = query(sql, params);
        try {
            if (resultSet == null){
                return null;
            }
            while (resultSet.next()){
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return role;
    }

    //主键查询(三表联查)
    @Override
    public Role findRoleById(int roleId) {
        Role role = new Role();
        List menuList = new ArrayList();
        String sql = "select * from role r,menu m,middle mid where r.roleid=mid.roleid and mid.menuid=m.menuid and r.roleid=?";
        List params = new ArrayList();
        params.add(roleId);
        resultSet = query(sql,params);
        try {
            while (resultSet.next()){
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUrl(resultSet.getString("url"));
                menu.setState(resultSet.getInt("state"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menuList.add(menu);
            }
            role.setMenuList(menuList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return role;
    }

    //新增角色
    @Override
    public int insert(Role role) {
        int key = 0;//保存新增数据的id
        try {
            String sql = "insert into role values(null,?,?)";
            List params = new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
            int i = update(sql,params);
            ResultSet generatedKeys = pps.getGeneratedKeys();
            if (generatedKeys.next()){
                key = generatedKeys.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return key;
    }

    //查询角色所对应的菜单id
    @Override
    public List<Integer> getRoleAndItsMenu(int roleId) {
        List<Integer> menuIds = new ArrayList<>();
        String sql = "select DISTINCT menuid from role r,middle mi where ?=mi.roleid";
        List params = new ArrayList();
        params.add(roleId);
        resultSet = query(sql,params);
        try {
            while (resultSet.next()){
                menuIds.add(resultSet.getInt(1));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return menuIds;
    }

    //修改角色信息
    @Override
    public int updateRole(Role role) {
        int i=0;
        try {
            String sql = "update role set rolename=?,rolestate=? where roleid=?";
            List params = new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
            params.add(role.getRoleId());
            i = update(sql,params);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return i;
    }

    //删除角色所对应的菜单列表(如果该角色没有权限则会返回0)
    @Override
    public int delRoleMenu(int roleId) {
        int successSign=0;
        try {
            String sql="DELETE FROM middle WHERE roleid=?";
            List params = new ArrayList();
            params.add(roleId);
            int i = update(sql,params);
            if (i >= 0){
                successSign=1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return successSign;
    }

    //删除角色(删除role表数据)
    @Override
    public int delRole(int roleId) {
        int i = 0;
        try{
            String sql = "DELETE FROM role WHERE roleid=?";
            List params = new ArrayList();
            params.add(roleId);
            i = update(sql,params);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return i;
    }
}
